package com.example.mybatis.sqlserver.springbootmybatissqlserver.repository.impl;

import com.example.mybatis.sqlserver.springbootmybatissqlserver.pojo.bean.CategoryBean;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.pojo.bean.CategoryPropertyNameBean;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.pojo.bean.CategoryPropertyValueBean;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.repository.ICategoryRepository;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.repository.mapper.ICategoryMapper;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.repository.mapper.ICategoryPropertyNameMapper;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.repository.mapper.ICategoryPropertyValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by hzy on 2019/2/20.
 */
@Repository
public class CategoryRepositoryImpl implements ICategoryRepository {
    @Autowired
    ICategoryMapper mapper;

    @Autowired
    ICategoryPropertyNameMapper propertyNameMapper;

    @Autowired
    ICategoryPropertyValueMapper propertyValueMapper;

    @Override
    public boolean addCategory(CategoryBean bean) {
        return mapper.insert(bean) > 0;
    }

    @Override
    public CategoryBean findCategoryById(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CategoryBean> findAllCategory() {
        return mapper.selectAll();
    }

    @Override
    public boolean updateCategory(CategoryBean bean) {
        return mapper.updateByPrimaryKeySelective(bean) > 0;
    }

    @Override
    public List<CategoryBean> findCategoryByParentId(String parentId) {
        return mapper.selectByParentId(parentId);
    }

    @Override
    public boolean deleteCategoryById(String categoryId) {
        return mapper.deleteByPrimaryKey(categoryId) > 0;
    }

    @Override
    public boolean softDeleteCategoryById(String categoryId) {
        return mapper.updateSoftDeleteFiled(categoryId,true) > 0;
    }

    @Override
    public List<CategoryPropertyNameBean> listCategoryPropertyName(String categoryId) {
        return propertyNameMapper.selectByFkCategory(categoryId);
    }

    @Override
    public boolean addCategoryProperty(CategoryPropertyNameBean bean) {
        return propertyNameMapper.insert(bean) > 0;
    }

    @Override
    public CategoryPropertyNameBean findCategoryPropertyById(String propertyId) {
        return propertyNameMapper.selectWithPropertyValueByPrimaryKey(propertyId);
    }

    @Override
    public boolean updateCategoryPropertyName(CategoryPropertyNameBean bean) {
        return propertyNameMapper.updateByPrimaryKeySelective(bean) > 0;
    }

    @Override
    public boolean addCategoryPropertyValue(CategoryPropertyValueBean valueBean) {
        return propertyValueMapper.insert(valueBean) > 0;
    }

    @Override
    public boolean updateCategoryPropertyValue(CategoryPropertyValueBean bean) {
        return propertyValueMapper.updateByPrimaryKeySelective(bean) > 0;
    }

    @Override
    public List<CategoryPropertyValueBean> listCategoryPropertyValue(String propertyId) {
        return propertyValueMapper.selectByPropertyNameId(propertyId);
    }

    @Override
    public String selectTime() {
        return mapper.selectTime();
    }

    @Override
    public List<CategoryBean> findCategoryByPaging( int offset, int pageSize) {
        return mapper.findCategoryByPaging(offset,pageSize);
    }
}
